﻿class PageFlipLite extends MovieClip {
	var PI:Number = Math.PI;
	var COS90:Number;
	var SIN90:Number;
	var pages:Array = ["page1.jpg", "page2.jpg", "page3.jpg", "page4.jpg"];
	var pageIdx:Number = 0;
	var frontPage:MovieClip;
	var backPage:MovieClip;
	var mask1:MovieClip;
	var mask2:MovieClip;
	var page_txt:TextField;
	var pageW:Number = 240;
	var pageH:Number = 320;
	var flipingTimeMS:Number = 1000;
	var flipingStartTimer:Number;
	var isFliping:Boolean = false;
	var loadListener:Object;
	var mcl:MovieClipLoader;
	function PageFlipLite() {
		init();
	}
	function init() {
		//常數
		COS90 = Math.cos(-0.5*PI);
		SIN90 = Math.sin(-0.5*PI);
		//遮罩
		mask1 = createEmptyMovieClip("mask1", 2001);
		mask2 = createEmptyMovieClip("mask2", 2002);
		//頁碼
		createTextField("page_txt", 3001, 0, 0, 20, 20);
		page_txt = this["page_txt"];
		page_txt.autoSize = true;
		var tf:TextFormat = new TextFormat();
		tf.color = 0xffffff;
		page_txt.setNewTextFormat(tf);
		//品質
		_quality = "HIGH";
		//載入外部頁面圖檔
		loadListener = {onLoadInit:mx.utils.Delegate.create(this, function (target_mc:MovieClip) {
			target_mc._width = pageW;
			target_mc._height = pageH;
			if (loadListener.loadType == "FIRST_PAGE") {
				updatePageText();
			} else if (loadListener.loadType == "NEXT_PAGE") {
				frontPage = eval("page"+pageIdx);
				backPage = eval("page"+(pageIdx+1));
				frontPage._visible = true;
				flipingStartTimer = getTimer();
				_quality = "MEDIUM";
				onEnterFrame = toNextPage_onEnterFrame;
			} else if (loadListener.loadType == "PREV_PAGE") {
				frontPage = eval("page"+(pageIdx-1));
				backPage = eval("page"+pageIdx);
				frontPage._visible = false;
				flipingStartTimer = getTimer();
				_quality = "MEDIUM";
				onEnterFrame = toPrevPage_onEnterFrame;
			}
		})};
		mcl = new MovieClipLoader();
		mcl.addListener(loadListener);
		//先載入第一張
		loadListener.loadType = "FIRST_PAGE";
		mcl.loadClip(pages[pageIdx], createEmptyMovieClip("page"+pageIdx, 1000+pageIdx));
		//按鍵
		var keyListener:Object = {onKeyUp:mx.utils.Delegate.create(this, function () {
			if (Key.getCode() == Key.LEFT) {
				nextPage();
			} else if (Key.getCode() == Key.RIGHT) {
				prevPage();
			}
		})};
		Key.addListener(keyListener);
	}
	//下一頁
	function nextPage() {
		if (pageIdx<pages.length-1 && !isFliping) {
			isFliping = true;
			var nextPageIdx:Number = pageIdx+1;
			loadListener.loadType = "NEXT_PAGE";
			mcl.loadClip(pages[nextPageIdx], this.createEmptyMovieClip("page"+nextPageIdx, this["page"+pageIdx].getDepth()-1));
		}
	}
	//下一頁
	function toNextPage_onEnterFrame() {
		var angle:Number = -PI*Math.min((getTimer()-flipingStartTimer)/flipingTimeMS, 1);
		angle = Math.max(angle, -PI);
		updateMask(angle);
		if (angle<=-PI) {
			frontPage.removeMovieClip();
			mask1.clear();
			mask2.clear();
			pageIdx++;
			onEnterFrame = null;
			isFliping = false;
			_quality = "HIGH";
			updatePageText();
		}
	}
	//上一頁
	function prevPage() {
		if (pageIdx>0 && !isFliping) {
			isFliping = true;
			var prevPageIdx:Number = pageIdx-1;
			loadListener.loadType = "PREV_PAGE";
			mcl.loadClip(pages[prevPageIdx], this.createEmptyMovieClip("page"+prevPageIdx, this["page"+pageIdx].getDepth()+1));
		}
	}
	//上一頁
	function toPrevPage_onEnterFrame() {
		var angle:Number = -PI*(1-Math.min((getTimer()-flipingStartTimer)/flipingTimeMS, 1));
		angle = Math.min(angle, 0);
		updateMask(angle);
		if (angle>=0) {
			frontPage.setMask(null);
			backPage.removeMovieClip();
			mask1.clear();
			mask2.clear();
			pageIdx--;
			onEnterFrame = null;
			isFliping = false;
			_quality = "HIGH";
			updatePageText();
		}
	}
	//翻頁效果
	function updateMask(angle:Number) {
		var pC:Object = {x:pageW*(1+angle/PI), y:pageH};
		//var pC:Object = {x:0, y:pageH};
		var pM:Object = {x:0, y:0};
		var pB:Object = null;
		var pO:Object = null;
		var pB2:Object = null;
		//頁角座標
		pM.x = pC.x+Math.cos(angle)*(pageW-pC.x);
		pM.y = pC.y+Math.sin(angle)*(pageW-pC.x);
		//頁角轉90度後，映射到邊界的交叉點    
		var x1:Number = COS90*(pC.x-pM.x)-SIN90*(pC.y-pM.y);
		var y1:Number = COS90*(pC.y-pM.y)+SIN90*(pC.x-pM.x);
		pB = {x:0, y:0};
		pB.x = pageW;
		pB.y = pM.y+(pB.x-pM.x)*y1/x1;
		if (pB.y<=0) {
			pB.y = 0;
			pB.x = pM.x+(pB.y-pM.y)*x1/y1;
		}
		//延伸出去的座標                           
		pO = {x:0, y:0};
		pO.x = pM.x+Math.cos(angle+0.5*PI)*pageH;
		pO.y = pM.y+Math.sin(angle+0.5*PI)*pageH;
		if (pO.x<pageW) {
			//外點座標，轉90度後，映射到邊界的交叉點
			x1 = COS90*(pO.x-pB.x)-SIN90*(pO.y-pB.y);
			y1 = COS90*(pO.y-pB.y)+SIN90*(pO.x-pB.x);
			pB2 = {x:0, y:0};
			pB2.y = 0;
			pB2.x = pO.x+(pB2.y-pO.y)*x1/y1;
		}
		//前頁遮罩                                                  
		mask1.clear();
		mask1.beginFill(0xff0000, 100);
		mask1.moveTo(pC.x, pC.y);
		mask1.lineTo(pM.x, pM.y);
		mask1.lineTo(pB.x, pB.y);
		if (pB.y>0) {
			mask1.lineTo(pB.x, 0);
		}
		mask1.lineTo(0, 0);
		if (pC.x>0) {
			mask1.lineTo(0, pC.y);
		}
		mask1.lineTo(pC.x, pC.y);
		mask1.endFill();
		//折頁遮罩
		mask2.clear();
		mask2.beginFill(0x999999, 100);
		mask2.moveTo(pC.x, pC.y);
		mask2.lineTo(pM.x, pM.y);
		mask2.lineTo(pB.x, pB.y);
		if (pO.x<pageW) {
			mask2.lineTo(pB2.x, pB2.y);
		}
		mask2.lineTo(pC.x, pC.y);
		mask2.endFill();
		frontPage.setMask(mask1);
		frontPage._visible = true;
	}
	//頁碼
	function updatePageText() {
		page_txt.text = (pageIdx+1)+"/"+pages.length;
		page_txt._x = pageW-page_txt._width;
		page_txt._y = pageH-page_txt._height;
	}
}
